<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Exception\CustomException;
use App\Kernel\Traits\DaoTrait;
use App\Kernel\Util\AgentJwtInstance;
use App\Model\Agent;


use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use function JmesPath\search;

/**
 * class
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class AgentDao extends Dao
{
    use DaoTrait;

    /**
     * @inheritDoc
     */
    public function model(): string
    {
        return Agent::class;
    }

    /**
     * 计算月份数量
     * @param array $map
     * @param int $first
     * @param int $last
     * @return mixed
     */
    public function monthCount(array $map,int $first, int $last)
    {
        return Agent::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->whereBetween('created_at', [$first, $last])
            ->count();
    }

    /**
     * delete
     * @param int $partner_id
     * @return mixed
     */
    public function delete(int $partner_id)
    {
        return Agent::query()->where('id',$partner_id)->delete();
    }

    /**
     * create
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return Agent::query()->create($data);
    }

    /**
     * 通过ID获取用户
     *
     * @param array $map
     * @return mixed
     */
    public function count(array $map)
    {
        return Agent::query()->where(function($query) use ($map) {
            return queryWhere($query, $map);
        })->count();
    }

    /**
     * 通过ID获取用户
     *
     * @param array $map
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function mapManyPage(array $map, bool $abnormal = true)
    {
        $result = Agent::query()->where(function($query) use ($map) {
            return queryWhere($query, $map);
        })->paginate(10);

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过ID获取用户
     *
     * @param array $map
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function mapMany(array $map, bool $abnormal = true)
    {
        $result = Agent::query()->where(function($query) use ($map) {
            return queryWhere($query, $map);
        })->get();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过ID获取用户
     *
     * @param array $map
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function map(array $map, bool $abnormal = true)
    {
        $result = Agent::query()->where(function($query) use ($map) {
            return queryWhere($query, $map);
        })->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过ID获取用户
     *
     * @param int $id ID
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $result = Agent::query()->where('id', $id)->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过手机号码登陆或创建用户
     *
     * @param string $phone 手机号码
     * @return mixed
     */
    public function loginByPhone(string $phone)
    {
        return Agent::query()->where(['phone' => $phone])->where('status',1)->first();
    }

    /**
     * update
     * @param array $data
     * @return int
     */
    public function update(array $data)
    {
        return Agent::query()->where('id', agentId())->update($data);
    }

    /**
     * update
     * @param int $agent_id
     * @param array $data
     * @return int
     */
    public function updateMap(int $agent_id,array $data)
    {
        return Agent::query()->where('id', $agent_id)->update($data);
    }

    /**
     * 通过手机号查询数据
     * @param string $phone
     * @param array $select
     * @return mixed
     */
    public function getPhoneByUser( string $phone, array $select)
    {
        $result = Agent::query()->where('phone', $phone)->select($select)->first();

        if( !$result ) {
            $this->error('USER.PHONE_FAIL');
        }

        return $result;
    }

    /**
     * 通过手机号查询代理
     * @param string $phone
     * @param array $select
     * @return mixed
     */
    public function getAgentByPhone(string $phone, array $select)
    {
        $result = Agent::query()->where('phone', $phone)->select($select)->first();
        return $result;
    }


    /**
     * 根据查询条件获取所有的代理商信息
     * @param array $map 对应关系
     * @param string $sort 排序
     * @param array $select 选择字段
     * @param int $paginate 每页页数
     * @return mixed
     */
    public function getAgentsByMap(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();

        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }

        // 或
        if (isset($map['or_field'])) {
            $model = $model->where(function (Builder $query) use($map) {
                foreach ($map['or_field'] as $field => $item) {
                    // 普通查询
                    if (!is_array($item)) {
                        $query->orWhere($field, $item);
                        continue;
                    }
                    // 其它方式
                    switch (strtolower($item[0])) {
                        case 'in':
                            $query->orWhereIn($field, $item[1]);
                            break;

                        default:
                            $query->orWhere($field, $item[0], $item[1]);
                    }
                }
            });

            unset($map['or_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
            $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

    /**
     * 通过ID更新数据
     * @param int $id
     * @param array $data
     * @return int
     */
    public function updateDataById(int $id, array $data)
    {
        return Agent::query()->where('id', $id)->update($data);

    }

    /**
     * 通过多条ID更新数据
     * @param array $ids
     * @param array $data
     * @return int
     */
    public function updateDataByIds(array $ids, array $data)
    {
        return Agent::query()->whereIn('id', $ids)->update($data);
    }

    /**
     * 检查代理商是否存在
     * @param int $id
     * @return bool
     */
    public function checkAgentExistById(int $id)
    {
        return Agent::query()->where('id', $id)->exists();
    }


    /**
     * 检查多个代理商是否存在
     * @param array $ids
     * @return bool
     */
    public function checkAgentsExistByIds(array $ids)
    {
        return Agent::query()->whereIn('id', $ids)->exists();
    }

    /**
     * 删除多个代理商
     * @param array $ids
     * @return int|mixed
     */
    public function deleteByIds(array $ids)
    {
        return Agent::query()->whereIn('id', $ids)->delete();
    }


}